home *** CD-ROM | disk | FTP | other *** search
- /*
- * Sample program of ways for computing with numbers larger than normal
- * written in Turbo C. This program computes the factorial of a number
- * for vaules of 'N' upto 3000. (i.e. K! for values of K from 0 to 3000+
- */
- #include <time.h>
- #include <dos.h>
-
- typedef int number[10000];
-
- number arr;
- float t1, t2, tt;
- int x, i, place, N, tens, ones;
- char bell;
- struct time start, end, elapsed;
-
- void Roundoff()
- {
- for (i = 0; i <= place - 1; i++)
- {
- while (((arr[i] / tens) > 0))
- {
- arr[i + ones] = arr[i + ones] + ((arr[i] / tens) % 10);
- ones = ones + 1;
- tens = tens * 10;
- }
- arr[i] = arr[i] % 10;
- ones = 1;
- tens = 10;
- }
- }
-
- void Multiply()
- {
- for (i = 0; i <= place; i++)
- arr[i] = arr[i] * x;
- }
-
- void Setplace()
- {
- if ((arr[place - 1] != 0) && (place < 10000)) place = place + (x * arr[place - 1] / 10);
- }
-
-
- main(argc,argv)
- int argc;
- char *argv[];
- { if (argc > 1)
- N = atoi(argv[1]);
- else
- { printf("\n");
- printf("Program to Calculate N! for up to 10000 digits in final answer\n");
- printf("\n");
- printf("Please enter the number to calculate N!\n");
- printf("N = ? ");
- scanf("%d",&N); }
- for (i = 0; i <= 10000; i++) arr[i] = 0;
- place = 4;
- tens = 10;
- ones = 1;
- bell = '\007';
- printf("%c\n",bell);
- arr[0] = 1;
- gettime(&start);
- for (x = 1; x <= N; x++)
- {
- Multiply();
- Roundoff();
- Setplace();
- }
- gettime(&end);
- t1 = ((((start.ti_hour * 60) + start.ti_min) * 60) + start.ti_sec)*100 +
- start.ti_hund;
- t2 = ((((end.ti_hour * 60) + end.ti_min) * 60) + end.ti_sec)*100 +
- end.ti_hund;
- tt = (t2 - t1)/100;
- printf("Start time was - %02d:%02d:%02d.%02d\n", start.ti_hour,
- start.ti_min, start.ti_sec, start.ti_hund);
- printf("Finish time was - %02d:%02d:%02d.%02d\n", end.ti_hour,
- end.ti_min, end.ti_sec, end.ti_hund);
- printf("Total elapsed time was %6.2f seconds\n",tt);
- printf("\n");
- printf("%d ! = \n",N);
- while (arr[place] == 0) place = place - 1;
- for (i = 0; i <= place; i++)
- {
- printf("%d",arr[place - i]);
- if (((place - i) % 3 == 0) && (i < place)) printf(",");
- if ((place - i) % 54 == 0) printf("\n");
- }
- printf("\n");
- printf("%d digits%c\n",place + 1,bell);
- }